package com.spider.lwlk.mongodb.analysize

import com.spider.lwlk.mongodb.workplace.Mongo
import org.jsoup.Jsoup
import org.jsoup.nodes.Document

class Analysize {

    private Random r = new Random()

    private static getImgPath() { './' }

    Analysize() {

        Mongo.withInstance({ ->

            def row = db.html.findOne()

            if(row) try { analysize(row); deleteRow(row) } catch (e) { }

            else Thread.sleep(10 * 1000);
        })
    }

    private analysize(def row) {

        Document doc = Jsoup.parse(row.html as String);

        def map = getArgs(doc)

        storeImg(map.imgName)

        save(map)
    }

    private getArgs(Document doc) {

        def isbn = (doc.select('ul.key.clearfix').text() =~ /ISBN：(\d{13})/)[0][1];

        def title = doc.select('div h1').first().attr('title')

        def auther = doc.select('div.messbox_info span').first().text()

        def desc = doc.select('#content textarea').text()
                .replace('\n', '').replace("\u3000", '').replace(' ', '').replace('\t', '')
                .replaceAll(/<br>|<br\/>|<p>|<\/p>/, '')

        def type = doc.select('.breadcrumb').text().split('>')[0 .. -2].join(':')

        def img = doc.select('#largePic').attr('src')

        def imgName = (img =~ /\/([^\/]+?)$/)[0][1]

        [ title: title, auther: auther, desc: desc, isbn: isbn, imgName: imgName, type: type,
          time: Calendar.getInstance().getTimeInMillis(), randKey: r.nextInt() ]
    }

    private storeImg(def imgName) {

        new File(getImgPath() + (imgName as String)).withOutputStream { OutputStream os ->

            def conn = new URL(img as String).openConnection()

            conn.setConnectTimeout(10 * 1000);

            InputStream is = conn.getInputStream()

            byte[] buff = new byte[4096];

            for(def len = 0; len != -1; len = is.read(buff)) { os.write(buff, 0, len) }

            is.close()
        }
    }

    private save(def map) {

        Mongo.withInstance {

            if(!db.type.fondOne([ type: map.type ])) db.type.insert([ type: map.type ])

            map.type = db.type.fondOne([ type: map.type ])._id

            db.book.insert(map)

            db."book$typeId".insert(map)
        }
    }

    private deleteRow(row) { db.html.delete([ _id: _id ]) }
}
